<?php

namespace Beerus\Modules\Designer\Actions;

use Encore\Admin\Actions\RowAction;
use Illuminate\Http\Request;
use Doraemon\model\DesignerEvaluation;
use Doraemon\service\Designer\DesignerEvaluation as sDesignerEvaluation;


class RecoverEvaluate extends RowAction
{
    public $name = '恢复评价';
    const TOAST_ALERT = '确认将该条评价恢复嘛?恢复后将把该条评价展示!';

    public function handle($id)
    {
        $update_data['status'] = DesignerEvaluation::STATUS_PASSED;
        //首先判断该用户对该设计师是否已经有一条正常的评价,如果有不允许恢复;
        $evaluate_info = DesignerEvaluation::getDesignerUidById($id);
        $uid = $evaluate_info['from_uid'];
        $designer_uid = $evaluate_info['to_uid'];
        //5.23 区分签约类型
        $normal_evaluation_info = DesignerEvaluation::getNormalEvaluationInfoBySign($uid,$designer_uid, $evaluate_info['sign_status']);
        if ($normal_evaluation_info){
            //如果已经有正常的评价了,不允许恢复
            return $this->response()->error('该用户已经评价过啦,无法再恢复评价啦o(TωT)o');
        }else{
            $evaluate_result = DesignerEvaluation::updateInfo($id,$update_data);
            if ($evaluate_info['type'] == DesignerEvaluation::TYPE_USER_EVALUATION) {
                //更新缓存,重新计算评价数
                sDesignerEvaluation::reCalculateEvaluation($designer_uid);
            }
            if ($evaluate_result){
                return $this->response()->success('恢复该评价成功(*^__^*)')->refresh();
            }else{
                return $this->response()->error('恢复该评价失败,请稍后重试o(TωT)o');
            }
        }


    }

    public function retrieveModel(Request $request)
    {
        if (!$key = $request->get('_key')) {
            return false;
        }

        return $key;
    }

    public function dialog()
    {
        $this->confirm(self::TOAST_ALERT);
    }
}
